﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AUV5.Common;
using AUV5.Model;
using AUV5.BLL;
using System.Text;
namespace AUV5.Web.Pages
{
    public partial class UserBasicInfo : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {

                    if (base.IsVerified)
                    {
                        //绑定省市
                        this.BindProvince();
                        this.BindCity();
                        //绑定行业、教育、收入、婚姻等信息
                        BindUserFieldData();

                        //初始化用户数据
                        DataBindDetail();
                    }
                    else
                    {
                        Response.Redirect(URL_INDEX);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void BindUserFieldData()
        {
            DataBindControl(ddlIndustry, "Industry", true);
            DataBindControl(ddlEducation, "Education", true);
            DataBindControl(ddlIncome, "Income", true);
            DataBindControl(ddlMarrige, "Marrige", true);
            DataBindControl(ckbHobby, "Favor", false);
        }

        #region  绑定生日的三个下拉列表
        void DataBindBirthday(DateTime dt)
        {
            //填充DropDownList 年
            int i = 0;
            ddlYear.Items.Clear();
            ddlYear.Items.Add(new ListItem("请选择", ""));
            for (i = DateTime.Now.Year; i >= 1900; i--)
            {
                ListItem item = new ListItem(i.ToString(), i.ToString());
                ddlYear.Items.Add(item);
            }

            //填充DropDownList 月
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add(new ListItem("请选择", ""));
            for (i = 1; i <= 12; i++)
            {
                string t = i < 10 ? "0" + i.ToString() : i.ToString();
                ListItem item = new ListItem(t, i.ToString());
                ddlMonth.Items.Add(item);
            }

            //填充DropDownList 日
            ddlDay.Items.Clear();
            ddlDay.Items.Add(new ListItem("请选择", ""));
            for (i = 1; i <= 31; i++)
            {
                string t = i < 10 ? "0" + i.ToString() : i.ToString();
                ListItem item = new ListItem(t, i.ToString());
                ddlDay.Items.Add(item);
            }


            //给年和月的onchange 绑定方法，用于处理闰年的情况
            ddlYear.Attributes["onchange"] = "OnSelectChange(" + ddlYear.ClientID + "," + ddlMonth.ClientID + "," + ddlDay.ClientID + ");";
            ddlMonth.Attributes["onchange"] = "OnSelectChange(" + ddlYear.ClientID + "," + ddlMonth.ClientID + "," + ddlDay.ClientID + ");";

            //判断用户是否修改过该字段信息
            if (dt != new DateTime(1900, 1, 1))
            {
                ddlYear.SelectedValue = dt.Year.ToString();
                ddlMonth.SelectedValue = dt.Month.ToString();
                ddlDay.SelectedValue = dt.Day.ToString();
            }
            else
            {
                ddlYear.SelectedValue = "";
                ddlMonth.SelectedValue = "";
                ddlDay.SelectedValue = "";
            }
        }
        #endregion

        #region  显示个人信息到页面(顺序参照页面)
        void DataBindDetail()
        {
            Model.UserInfo user = base.VerifiedInfo.User;

            //用户名及邮箱不可修改
            lbEmail.Disabled = true;
            lbUserName.Disabled = true;

            lbEmail.Value = user.Email;
            lbUserName.Value = user.UserName;
            txbDescription.Text = user.Description.HTMLDeCode();
            txtRealName.Text = user.RealName.HTMLDeCode();
            //性别
            DataBindGender();
            //生日
            DataBindBirthday(user.Birthday);
            //显示用户省信息
            if (this.ddlProvince.Items.FindByValue(user.Province) != null)
            {
                this.ddlProvince.SelectedItem.Selected = false;
                this.ddlProvince.Items.FindByValue(user.Province).Selected = true;
            }
            this.BindCity();
            if (this.ddlCity.Items.FindByValue(user.City) != null)
            {
                this.ddlCity.SelectedItem.Selected = false;
                this.ddlCity.Items.FindByValue(user.City).Selected = true;
            }
            txbAddress.Text = user.Address.HTMLDeCode();

            txbPhone.Text = user.PhoneNo;
            txtQQ.Text = user.QQNo;

            //行业、教育、收入、婚姻等
            UserField userField = UserFieldBLL.Instance.GetDetailByUserID(base.VerifiedInfo.User.UserID);
            if (userField != null)
            {
                ddlIndustry.SelectedValue = userField.Industry;
                ddlEducation.SelectedValue = userField.Education;
                ddlIncome.SelectedValue = userField.Income;
                ddlMarrige.SelectedValue = userField.Marrige;

                string hobby = userField.Hobby;
                if (!string.IsNullOrEmpty(hobby))
                {
                    string[] selectedlist = hobby.Split(',');
                    foreach (ListItem item in ckbHobby.Items)
                    {
                        foreach (string selectedvalue in selectedlist)
                        {
                            if (item.Value == selectedvalue)
                            {
                                item.Selected = true;
                            }
                        }
                    }
                }
            }
            ckbReceive.Checked = user.ReceiveDigest;
        }
        #endregion

        #region 省、市
        private void BindProvince()
        {
            base.SetGBAD(this.ddlProvince, "1");
            this.ddlProvince.Items.Insert(0, new ListItem("请选择", ""));
            this.ddlProvince.Items[0].Selected = true;
        }

        private void BindCity()
        {
            if (!string.IsNullOrEmpty(this.ddlProvince.SelectedValue))
            {
                this.SetGBAD(this.ddlCity, this.ddlProvince.SelectedValue);
            }
            else
            {
                this.ddlCity.Items.Clear();
                this.ddlCity.Items.Insert(0, new ListItem("请选择", ""));
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindCity();
        }
        #endregion

        #region 性别
        void DataBindGender()
        {
            rbtnGender.DataSource = AUV5.Common.PageBase.GetEnumList<AUV5.Common.Enum.Gender>();
            rbtnGender.DataTextField = "Name";
            rbtnGender.DataValueField = "Value";
            rbtnGender.DataBind();
            foreach (ListItem item in rbtnGender.Items)
            {
                if (base.IsVerified)
                {
                    int gender = base.VerifiedInfo.User.Gender;

                    if (gender == Convert.ToInt32(item.Value))
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }
        #endregion

        #region 行业、教育、收入、婚姻等
        void DataBindControl(ListControl control, string dataKey, bool hasDefault)
        {
            IList<BaseData> list = BaseDataBLL.Instance.GetListByDataKey(dataKey);
            control.DataSource = list;

            control.DataTextField = "DataValue";
            control.DataValueField = "DataID";
            control.DataBind();
            if (hasDefault)
            {
                control.Items.Insert(0, new ListItem("==请选择==", "0"));
            }
        }
        #endregion

        #region 更新个人信息
        protected void imgbtnSave0_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (base.IsVerified)
                {
                    Model.UserInfo user = base.VerifiedInfo.User;
                    UserField userField = UserFieldBLL.Instance.GetDetailByUserID(user.UserID);

                    //创建不存在的记录
                    if (userField == null)
                    {
                        userField = new UserField();
                        userField.FieldID = Guid.NewGuid().ToString();
                        userField.UserID = user.UserID;
                        UserFieldBLL.Instance.Add(userField);
                    }

                    user.Email = lbEmail.Value.Trim();
                    user.UserName = lbUserName.Value.Trim();
                    string description = txbDescription.Text.Trim();

                    //敏感字符过滤
                    BadWordsFilter.Instance.FindAndReplace(ref description, '*');
                    user.Description = description;

                    user.RealName = txtRealName.Text.Trim();
                    user.Gender = Convert.ToInt32(rbtnGender.SelectedValue);

                    //todo:
                    if (ddlYear.SelectedValue == "" || ddlMonth.SelectedValue == "" || ddlDay.SelectedValue == "")
                    {

                    }
                    else
                    {
                        DateTime dt = new DateTime(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue), Convert.ToInt32(ddlDay.SelectedValue));
                        user.Birthday = dt;
                    }

                    user.Province = ddlProvince.SelectedValue;
                    user.City = ddlCity.SelectedValue;
                    user.Address = txbAddress.Text.Trim();
                    user.PhoneNo = txbPhone.Text.Trim();
                    user.QQNo = txtQQ.Text.Trim();

                    userField.Industry = ddlIndustry.SelectedValue;
                    userField.Education = ddlEducation.SelectedValue;
                    userField.Income = ddlIncome.SelectedValue;
                    userField.Marrige = ddlMarrige.SelectedValue;


                    StringBuilder buffer = new StringBuilder();
                    foreach (ListItem item in ckbHobby.Items)
                    {
                        if (item.Selected)
                        {
                            buffer.Append(item.Value);
                            buffer.Append(",");
                        }
                    }
                    string hobbyStr = buffer.ToString();
                    if (!string.IsNullOrWhiteSpace(hobbyStr))
                    {
                        hobbyStr = hobbyStr.Substring(0, hobbyStr.Length - 1);
                        userField.Hobby = hobbyStr;
                    }

                    user.ReceiveDigest = user.ReceiveDigest;

                    //更新信息
                    if (BLL.UserInfoBLL.Instance.Update(user) == 1 && UserFieldBLL.Instance.Update(userField) == 1)
                    {
                        result.Visible = true;
                        result.InnerHtml = "更新成功";
                    }
                    //base.Alert("更新成功");
                    else
                    {
                        result.Visible = false;
                        result.InnerHtml = "更新失败";
                        result.Attributes.Add("class", "errorinfo");
                        //base.Alert("更新失败");
                    }
                }
                else
                {
                    Response.Redirect(URL_LOGIN);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {

            }
        }
        #endregion

       
    }
}